package com.android.Translator.View;

import java.util.ArrayList;
import java.util.Arrays;

import android.R.color;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import com.android.Translator.R;
import com.android.Translator.Presenter.TranslatorPresenter;
import com.android.Translator.View.utils.ViewUtils;

public class AddWordToLearn extends Activity implements OnClickListener{

	private Spinner m_SpinnerLanguage, m_SpinnerGroup;
	private ImageButton m_imageButton;
	private EditText m_EditWord;
	private Button btnAdd;

	private static ProgressDialog mProgressDialog;
	  
	private static AddWordToLearn m_activity;
	private ArrayList<String> groupNameList = new ArrayList<String>();
	private String[] ListLanguage = {"VietNamese", "English"};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_word_to_learn);
		m_activity = this;
		initView();
		
		ArrayList<String> tmp = new ArrayList<String>(Arrays.asList(ListLanguage));
		SetArrayListToSpinner(m_SpinnerLanguage, tmp);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		groupNameList = TranslatorPresenter.getGroupList(this);
		SetArrayListToSpinner(m_SpinnerGroup, groupNameList);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	private void initView()
	{
		mProgressDialog = new ProgressDialog(m_activity, R.style.Theme_ProgressDialog);
		mProgressDialog.setIndeterminate(false);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setMessage("Please Wait....");
		m_EditWord = (EditText) findViewById(R.id.editWord);
		btnAdd = (Button)findViewById(R.id.btnAdd);
		btnAdd.setOnClickListener(this);
		m_SpinnerLanguage = (Spinner) findViewById(R.id.spinLanguage);
		m_SpinnerGroup = (Spinner) findViewById(R.id.spinner_Choose_Group);
		m_imageButton = (ImageButton) findViewById(R.id.imgBtnAddGroup);
		m_imageButton.setOnClickListener(this);
	}
	
	
	private void SetArrayListToSpinner(Spinner spinner, ArrayList<String> list)
	{
		ArrayAdapter<String> apt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, 
														android.R.id.text1, list);
		spinner.setAdapter(apt);
		spinner.setSelection(list.size()-1);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch (id)
		{
		case R.id.btnAdd:
			String editGroup = "";
			if(groupNameList.size()>0 && !m_EditWord.getText().toString().equals(""))
			{
				editGroup = m_SpinnerGroup.getSelectedItem().toString();
				showProcessCircle();
				TranslatorPresenter.TranslateWordAdd(m_activity, m_EditWord.getText().toString(), editGroup);
			}
			else
				ViewUtils.ShowAlertDialog(AddWordToLearn.this, null , ViewUtils.ACTION_INFORM, 	
						getString(R.string.TITLE_ALERT_DIALOG), 
						getString(R.string.MESSAGE_INPUT_OR_GROUP_EMPTY));
			m_EditWord.setText("");
			break;
		case R.id.imgBtnAddGroup:
			Intent i = new Intent(AddWordToLearn.this, AddWordGroup.class);
			startActivity(i);
			break;
		}
	}

	public static void showFinishDialog() {
		// TODO Auto-generated method stub
		ViewUtils.ShowAlertDialog(m_activity, null , ViewUtils.ACTION_FINISH, 	
				m_activity.getString(R.string.TITLE_ALERT_DIALOG), 
				m_activity.getString(R.string.MESSAGE_ADD_WORD_DONE));
	}

	public static void showProcessCircle() {
		// TODO Auto-generated method stub
		 mProgressDialog.show();
		
	}
	public static void dismissProcessCircle() {
		mProgressDialog.dismiss();
	}
}
